//
//  NSString+SFEmoji.m
//  SFEmoticon
//
//  Created by 许博 on 16/7/18.
//  Copyright © 2016年 许博. All rights reserved.
//

#import "NSString+SFEmoji.h"

@implementation NSString (SFEmoji)
#define EmojiCodeToSymbol(c) ((((0x808080F0 | (c & 0x3F000) >> 4) | (c & 0xFC0) << 10) | (c & 0x1C0000) << 18) | (c & 0x3F) << 24)

+ (NSString *)sf_emojiWithIntCode:(unsigned int)intCode {
    unsigned int symbol = EmojiCodeToSymbol(intCode);
    NSString *string = [[NSString alloc] initWithBytes:&symbol length:sizeof(symbol) encoding:NSUTF8StringEncoding];
    
    if (string == nil) {
        string = [NSString stringWithFormat:@"%C", (unichar)intCode];
    }
    return string;
}

+ (NSString *)sf_emojiWithStringCode:(NSString *)stringCode {
    
    NSScanner *scanner = [[NSScanner alloc] initWithString:stringCode];
    
    unsigned int intCode = 0;
    [scanner scanHexInt:&intCode];
    
    return [self sf_emojiWithIntCode:intCode];
}

- (NSString *)sf_emoji {
    return [NSString sf_emojiWithStringCode:self];
}

@end
